﻿ #region Disclaimer/Info
 
 /////////////////////////////////////////////////////////////////////////////////////////////////
 //
 //   File:		BlogRollRepository.cs
 //   Website:		http://dexterblogengine.com/
 //   Authors:		http://dexterblogengine.com/About.ashx
 //   Rev:		1
 //   Created:		19/01/2011
 //   Last edit:		19/01/2011
 //   License:		GNU Library General Public License (LGPL)
 //   File:            BlogRollRepository.cs
 //   For updated news and information please visit http://dexterblogengine.com/
 //   Dexter is hosted to Codeplex at http://dexterblogengine.codeplex.com
 //   For any question contact info@dexterblogengine.com
 //
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
 #endregion

using System;
using System.Collections.Generic;
using System.Linq;
using Dexter.Core.Data.Repository;
using Dexter.Domain.Model;
using Dexter.Domain.Results;
using Dexter.Logger.Contracts;
using Dexter.Repository.NHibernate.Queries;
using NHibernate;
using Topics.Radical.Validation;
using ISessionFactory = Dexter.Repository.NHibernate.SessionFactory.ISessionFactory;

namespace Dexter.Repository.NHibernate.Repositories {

	/// <summary>
	/// The implementation of <see cref="IBlogRollRepository"/>.
	/// </summary>
	/// <example>The following is an example of initializing 
	///          an <c>BlogRollRepository</c> type:
	///   <code>
	///       // Create the type.
	///       BlogRollRepository ds = new BlogRollRepository;
	/// 
	///       if ( null == ds )
	///           return;
	/// 
	///       return ds.DoSomething( 5 );
	///   </code>
	/// </example>
	public class BlogRollRepository : RepositoryBase <BlogRoll> , IBlogRollRepository {
		/// <summary>
		/// Initializes a new instance of the <see cref="BlogRollRepository"/> class.
		/// </summary>
		/// <param name="sessionFactory">The session factory.</param>
		/// <param name="logger">The logger.</param>
		public BlogRollRepository ( ISessionFactory sessionFactory , ILogger logger ) : base ( sessionFactory , logger ) {}

		#region Implementation of IBlogRollRepository

		/// <summary>
		/// Return a list of BlogRoll with all properties loaded.
		/// </summary>
		/// <param name="pageIndex">Index of the page (0 based).</param>
		/// <param name="pageSize">The number of items to retrieve.</param>
		/// <returns>
		/// An concrete implementation of <see cref="IPagedResult"/>. The result will never be null.
		/// </returns>
		/// <exception cref="ArgumentOutOfRangeException"> If <paramref name="pageIndex"/> is lesser than 0.</exception>
		/// <exception cref="ArgumentOutOfRangeException"> If <paramref name="pageSize"/> is lesser than 1.</exception>
		public virtual IPagedResult <BlogRoll> GetListPaged ( int pageIndex , int pageSize ) {
			return GetListPaged ( pageIndex , pageSize , true );
		}

		/// <summary>
		/// Return a list of BlogRoll with all properties loaded.
		/// </summary>
		/// <param name="pageIndex">Index of the page (0 based).</param>
		/// <param name="pageSize">The number of items to retrieve.</param>
		/// <param name="enableTracking">if set to <c>true</c> the tracking system of the return object will be enable.</param>
		/// <returns>
		/// An concrete implementation of <see cref="IPagedResult{T}"/>. The result will never be null.
		/// </returns>
		/// <exception cref="ArgumentOutOfRangeException"> If <paramref name="pageIndex"/> is lesser than 0.</exception>
		/// <exception cref="ArgumentOutOfRangeException"> If <paramref name="pageSize"/> is lesser than 1.</exception>
		public virtual IPagedResult<BlogRoll> GetListPaged ( int pageIndex, int pageSize, bool enableTracking ) {
			Ensure.That ( pageIndex )
				.Named ( "pageIndex" )
				.WithPreview ( ( v , e ) => Logger.Warn ( v.GetFullErrorMessage ( ) , e ) )
				.IsGreaterThen ( 0 , Or.Equal )
				.WithMessage ( "Invalid page index." );

			Ensure.That ( pageSize )
				.Named ( "pageSize" )
				.WithPreview ( ( v , e ) => Logger.Warn ( v.GetFullErrorMessage ( ) , e ) )
				.IsGreaterThen ( 0 )
				.WithMessage ( "Invalid page size." );

			IFutureValue <long> total = Session.GetNamedQuery ( KnownQueries.BlogRollRepository.GetBlogRollCount )
				.FutureValue <long> ( );

			IEnumerable <BlogRoll> result = Session.GetNamedQuery ( KnownQueries.BlogRollRepository.GetBlogRollOrdedByPosition )
				.SetFirstResult ( pageIndex * pageSize )
				.SetMaxResults ( pageSize )
				.SetReadOnly ( !enableTracking )
				.Future <BlogRoll> ( );

			return new PagedResult<BlogRoll>(pageIndex, pageSize, result.ToList(), total.Value);
		}

		/// <summary>
		/// Adds the specified item.
		/// </summary>
		/// <param name="item">The item.</param>
		public override void Add ( BlogRoll item ) {
			this.Save ( item );
		}

		/// <summary>
		/// Removes the specified item.
		/// </summary>
		/// <param name="item">The item.</param>
		/// <returns></returns>
		public override bool Remove ( BlogRoll item ) {
			this.Delete ( item );
			return true;
		}

		/// <summary>
		/// Saves the specified item into the repository.
		/// </summary>
		/// <param name="item">The item.</param>
		/// <exception cref="ArgumentOutOfRangeException"> If <paramref name="item"/> is <c>null</c>.</exception>
		public virtual void Save ( BlogRoll item ) {
			Ensure.That ( item )
				.Named ( "item" )
				.WithPreview ( ( v , e ) => Logger.Warn ( v.GetFullErrorMessage ( ) , e ) )
				.IsNotNull ( )
				.WithMessage ( "Invalid blog roll" );

			item.ValidateForSave ( );
			Session.SaveOrUpdate ( item );
		}

		/// <summary>
		/// Updates the specified item into the repository.
		/// </summary>
		/// <param name="item">The item.</param>
		/// <exception cref="ArgumentOutOfRangeException"> If <paramref name="item"/> is <c>null</c>.</exception>
		public virtual void Update ( BlogRoll item ) {
			Ensure.That ( item )
				.Named ( "item" )
				.WithPreview ( ( v , e ) => Logger.Warn ( v.GetFullErrorMessage ( ) , e ) )
				.IsNotNull ( )
				.WithMessage ( "Invalid blog roll" );

			item.ValidateForUpdate ( );
			Session.SaveOrUpdate ( item );
		}

		/// <summary>
		/// Deletes the a blog roll from the repository.
		/// </summary>
		/// <param name="item">The item.</param>
		/// <exception cref="ArgumentOutOfRangeException"> If <paramref name="item"/> is <c>null</c>.</exception>
		public virtual void Delete ( BlogRoll item ) {
			Ensure.That ( item )
				.Named ( "item" )
				.WithPreview ( ( v , e ) => Logger.Warn ( v.GetFullErrorMessage ( ) , e ) )
				.IsNotNull ( )
				.WithMessage ( "Invalid blog roll" );

			item.ValidateForDelete ( );

			Session.Delete ( item );
		}

		#endregion
	}
}
